// Gradient Maker Rev.A, by Dave Bushnell
// ZBrush ZScript v1.23b 

[varDef,transData(9)]
[varDef,sqrt2,sqrt(2)]
[varDef,NumUndos,0]
[varDef,SettingsData(12)]
[varDef,ShowMessage,1]
[varDef,ButtonBlink,0]
[varDef,ButtonBlinkDelay,1900]
[varDef,OrthoSnap,0]
[varDef,UseCurrentAlpha,0]
[varDef,ButtonText(7)]
[varSet,ButtonText(0)," Create Linear Gradient "]
[varSet,ButtonText(1)," Create Opposite Gradient  "]
[varSet,ButtonText(2)," Click at the start of the gradient, drag to the end "]
[varSet,ButtonText(3)," Click at the center of the gradient, drag to the outside   "]
[varSet,ButtonText(4)," Show and Tell "]
[varSet,ButtonText(5),"Note: this ZScript will not work on an empty canvas."]
[varSet,ButtonText(6),"                         Modifiers                         "]
[fontSetSizeSmall][varSet,ButtonWidth1,([TextCalcWidth,[var,ButtonText(1)]])]
[varSet,ButtonWidth2,([TextCalcWidth,[var,ButtonText(3)]])][fontSetSizeMedium]
[PenMove,0,2][Image,GMTitle.psd]
[PenMove,15,3][ibutton,"Return to Menu","Load the Script Selection Interface",[fileNameSetNext,"DefaultZScript.txt"][iPress,7427]]
[pd][penMove,,7][caption,ButtonText(5)]
[pd][penMove,,4]
[penMoveCenter][penMove,-ButtonWidth2/2][iButton," Create Linear Gradient ","Create a linear color/texture gradient from 100% intensity (click) to 0% (release)",
	[loop,9999, // make sure mouse starts unclicked
		[if,[mouseLbutton] = 0,
			[loopexit]
		] // end if
	] // end wait for mouse unclicked loop
	[varSet,ButtonBlink,0]
	[loop,999999, // wait for mouse click
		[if,[mouseLbutton] = 1,
			[varSet,Hval1,[mouseHpos]]
			[varSet,Vval1,[mouseVpos]]
			[loopexit]
		] // end if
		[varInc,ButtonBlink]
		[if,ButtonBlink > ButtonBlinkDelay,
			[iSetStatus,zscript:lineargradientmessage,abs([iGetStatus,zscript:lineargradientmessage]-1)]
			[varSet,ButtonBlink,0]
		] // end if
	] // end wait for mouse click loop
	[loop,999999, // now wait for mouse up
		[if,[mouseLbutton] = 0,
			[varSet,Hval2,[mouseHpos]]
			[varSet,Vval2,[mouseVpos]]
			[loopexit]
		] // end if
	] // end wait for mouse up loop

	[iDisable,zscript:lineargradientmessage]
	
	[routineCall,StoreSettings]
	[routineCall,ZeroAdjust,Hval1,Vval1,Hval2,Vval2]
	[routineCall,DrawLinearGradient,Hval1,Vval1,Hval2,Vval2]
	[routineCall,UndoLinearGradient]
	[routineCall,RecallSettings]

	[iEnable,1]
	[iSet,zscript:startX,Hval1]
	[iSet,zscript:startY,Vval1]
	[iSet,zscript:endX,Hval2]
	[iSet,zscript:endY,Vval2]
,0,ButtonWidth1] // end iButton

[penMoveCenter][penMove,ButtonWidth2/2][penMove,-ButtonWidth1][iButton,[var,ButtonText(1)],"Use the information from the previous Linear gradient and fill the rest of the canvas",
	[routineCall,StoreSettings]
	[routineCall,ZeroAdjust,Hval1,Vval1,Hval2,Vval2]
	[routineCall,DrawLinearGradient,Hval2,Vval2,Hval1,Vval1]
	[routineCall,UndoLinearGradient]
	[routineCall,RecallSettings]

,1,ButtonWidth1] // end iButton
[pd]
[penMoveCenter][PenMove,-ButtonWidth2/2][iButton,[var,ButtonText(2)],"Linear gradient message",[delay,.1],1,ButtonWidth2]
[pd]
[penMoveCenter][penMove,-ButtonWidth2/2][iButton," Create Radial Gradient","Create a radial color/texture gradient from center 100% intensity (click) to outside 0% (release)",
	[loop,9999, // make sure mouse starts unclicked
		[if,[mouseLbutton] = 0,
			[loopexit]
		] // end if
	] // end wait for mouse unclicked loop
	[varSet,ButtonBlink,0]
	[loop,999999, // wait for mouse click
		[if,[mouseLbutton] = 1,
			[varSet,Hval1,[mouseHpos]]
			[varSet,Vval1,[mouseVpos]]
			[loopexit]
		] // end if
		[varInc,ButtonBlink]
		[if,ButtonBlink > ButtonBlinkDelay,
			[iSetStatus,zscript:radialgradientmessage,abs([iGetStatus,zscript:radialgradientmessage]-1)]
			[varSet,ButtonBlink,0]
		] // end if
	] // end wait for mouse click loop
	[loop,999999, // now wait for mouse up
		[if,[mouseLbutton] = 0,
			[varSet,Hval2,[mouseHpos]]
			[varSet,Vval2,[mouseVpos]]
			[loopexit]
		] // end if
	] // end wait for mouse up loop
	[iDisable,zscript:radialgradientmessage]
	
	[routineCall,StoreSettings]
	[routineCall,ZeroAdjust,Hval1,Vval1,Hval2,Vval2]
	[routineCall,DrawRadialGradient,Hval1,Vval1,Hval2,Vval2]
	[routineCall,UndoRadialGradient]
	[routineCall,RecallSettings]

,0,ButtonWidth1] // end iButton
[penMoveCenter][penMove,ButtonWidth2/2][penMove,-ButtonWidth1][iButton,[var,ButtonText(4)],"Brief tutorial",
	[routineCall,StoreSettings]
	[iPress,layer:inventory:createnewlayer]
	[varSet,docWidth,document:modifiers:width]
	[varSet,docHeight,document:modifiers:height]
	[Note,"This ZScript adds a texture or color gradient to the canvas without adding depth.",,10]
	[Note,"Therefore, before pressing any buttons, you must make sure there is something drawn on the canvas first.",,14]
	[Note,"Begin by selecting Texture Off",texture:txtr00,-1]
	[Note,"\n\nand choosing a color...",color:,10]
	[iSet,texture:itemInfo,0]
	[iSet,material:itemInfo,1]
	[iColorSet,0,0,255]
	[Note,"...and filling the canvas.",layer:modifiers:fill,10]
	[iPress,layer:modifiers:fill]
	[iSet,layer:modifiers:displacez,-4000]
	[Note,"To draw a Linear gradient, choose another color...",color:,10]
	[iColorSet,255,0,0]
	[Note,"...and press the Create Linear Alpha button.",zscript:createlineargradient,10]
	[Note,"Then, click at the place you'd like your Linear gradient to be strongest, drag to the place you'd like it to fade away completely, and release the mouse button.",,14]
	[varSet,HVal1,50][varSet,VVal1,50]
	[varSet,HVal2,docWidth-50][varSet,VVal2,docHeight-50]
	[routineCall,DrawLinearGradient,HVal1,VVal1,HVal2,VVal2]
	[Note,"Your Linear gradient is done.",1004,0]
	[iKeyPress,'N',[routineCall,UndoLinearGradient]]
	
	[Note,"You can also create a gradient using a texture. A seamlessly tileable texture is recommended.",texture,10]
	[Note,"Choose a texture...",texture:,10]
	[iSet,texture:itemInfo,3]
	[Note,"...and create a gradient as before.",zscript:createlineargradient,10]
	[varSet,HVal1,50][varSet,VVal1,50]
	[varSet,HVal2,docWidth-50][varSet,VVal2,docHeight-50]
	[routineCall,DrawLinearGradient,HVal1,VVal1,HVal2,VVal2]
	[Note,"Your Linear gradient is done.",1004,0]
	[iKeyPress,'N',[routineCall,UndoLinearGradient]]
	
	[Note,"To draw a Radial gradient, the process is the same...",zscript:drawradialgradient,10]
	[Note,"...except this time, you'll click at the center of the gradient, where the color/texture is strongest, and drag to the outside of the gradient before releasing.",,14]
	[iSet,texture:itemInfo,0]
	[varSet,HVal1,docWidth/2][varSet,VVal1,docHeight/2]
	[varSet,HVal2,docWidth/2+50][varSet,VVal2,docHeight-50]
	[routineCall,DrawRadialGradient,HVal1,VVal1,HVal2,VVal2]
	[Note,"Your Radial gradient is done.",1004,0]
	[iKeyPress,'N',[routineCall,UndoRadialGradient]]
	[iKeyPress,13,[iPress,layer:inventory:deletelayer]]
	[routineCall,RecallSettings]
,0,ButtonWidth1] // end iButton

[pd]
[penMoveCenter][PenMove,-ButtonWidth2/2][iButton,[var,ButtonText(3)],"Radial gradient message",[delay,.1],1,ButtonWidth2]

[FontSetSizeSmall][pd][penMove,0,6][penMoveCenter][penMove,-[TextCalcWidth,[var,ButtonText(6)]]/2][sectionBegin,[var,ButtonText(6)],0][FontSetSizeMedium]
[pd][penMove,0,8]
[penMoveCenter][penMove,-ButtonWidth2/2][iSlider,"Start X",0,.1,-4096,4096,"X component of Starting Point",,0,(ButtonWidth2-30)/4]
[iSlider,"Start Y",0,.1,-4096,4096,"Y component of Starting Point",,0,(ButtonWidth2-30)/4]
[penMove,30][iSlider,"End X",0,.1,-4096,4096,"X component of Ending Point",,0,(ButtonWidth2-30)/4]
[iSlider,"End Y",0,.1,-4096,4096,"Y component of Ending Point",,0,(ButtonWidth2-30)/4]
[pd][penMoveCenter][penMove,-ButtonWidth2/2][iButton," Draw Linear Gradient ","Draw a linear gradient using the above sliders",
	[varSet,Hval1,[iGet,zscript:startx]]
	[varSet,Vval1,[iGet,zscript:starty]]
	[varSet,Hval2,[iGet,zscript:endx]]
	[varSet,Vval2,[iGet,zscript:endy]]
		
	[routineCall,StoreSettings]
	[routineCall,ZeroAdjust,Hval1,Vval1,Hval2,Vval2]
	[routineCall,DrawLinearGradient,Hval1,Vval1,Hval2,Vval2]
	[routineCall,UndoLinearGradient]
	[routineCall,RecallSettings]

,0,ButtonWidth2/2] // end iButton
[iButton," Draw Radial Gradient ","Draw a radial gradient using the above sliders",
	[varSet,Hval1,[iGet,zscript:startx]]
	[varSet,Vval1,[iGet,zscript:starty]]
	[varSet,Hval2,[iGet,zscript:endx]]
	[varSet,Vval2,[iGet,zscript:endy]]
		
	[routineCall,StoreSettings]
	[routineCall,ZeroAdjust,Hval1,Vval1,Hval2,Vval2]
	[routineCall,DrawRadialGradient,Hval1,Vval1,Hval2,Vval2]
	[routineCall,UndoRadialGradient]
	[routineCall,RecallSettings]

,0,ButtonWidth2/2] // end iButton

[pd][penMove,0,6][penMoveCenter][penMove,-ButtonWidth2/2][iSwitch," Snap H/V ",0,"Snap to horizontal or vertical (if pressed)",
	[varSet,OrthoSnap,1]
,
	[varSet,OrthoSnap,0]
,0,ButtonWidth2/2] // end iSwitch
[iSwitch," Hide Confirm Message ",0,"Hide 'Are You Satisfied' Message (if pressed)",
	[varSet,ShowMessage,0]
,
	[varSet,ShowMessage,1]
,0,ButtonWidth2/2] // end iSwitch
[pd][penMove,0,6][penMoveCenter][penMove,-ButtonWidth2/2][iSwitch," Use Selected Alpha ",0,"If pressed, the Create Radial Gradient button draws using the selected alpha.",
	[varSet,UseCurrentAlpha,1]
,
	[varSet,UseCurrentAlpha,0]
,0,ButtonWidth2/2] // end iSwitch

[sectionEnd][pd][pd][FontSetSizeMedium]
\C00e0e0About This ZScript:
\Cc0c0c0This ZScript can be loaded at any time while you're working. It enables you to create a Linear or Radial gradient from one point to another. This script adds color or texture without applying material or depth information, so you must have something drawn on the canvas before you can see any results.

The gradient drawn goes from full-strength to nothing. Therefore, if your objective is to blend one color into another, first fill the canvas with the second color, then use this ZScript.

Also, you can add noise to your gradient using the [buttonFind,alpha:noise," Alpha Noise "] button. A small amount of noise (3-4 units) is a good way to visually compensate for color banding that sometimes occurs.

\C00e0e0Linear Gradients:
\Cc0c0c0To create a linear gradient, choose a color or texture and press the \Cffa000Create Linear Gradient\Cc0c0c0 button. Then click your mouse/tablet button at the point where you'd like the gradient to begin (where the current color/texture is strongest), and drag to where you'd like it to end (where the current color/texture fades to nothing).

From the edge of the canvas to your chosen start point, this ZScript will fill the current color or texture at full strength. Using a seamless repeatable texture is recommended for best results.

After drawing the linear gradient, you can create another gradient that fills the remainder of the canvas. First choose another color or texture, then press the \Cffa000Create Opposite Gradient\Cc0c0c0 button.

\C00e0e0Radial Gradients:
\Cc0c0c0To create a radial gradient, choose a color or texture and press the \Cffa000Create Radial Gradient\Cc0c0c0 button. Then click your mouse/tablet button at the center point of the gradient (where the color/texture is strongest), and drag to the edge of the gradient (where the color/texture fades to nothing). The end point is assumed to be the right side of the gradient, so if you've chosen a texture that has a left and right side, it will be rotated so the right side is positioned where you release the mouse/tablet button.

The \Cffa000Show and Tell\Cc0c0c0 button gives you a brief tutorial on how this ZScript is used.

\C00e0e0About the Modifiers:
\Cc0c0c0You can enter data numerically by using sliders in this area.

If the \Cffa000Snap H/V\Cc0c0c0 switch is pressed, the gradient will only be drawn horizontally or vertically.

Press the \Cffa000Hide Confirm Message\Cc0c0c0 switch to inhibit the confirmation message that normally appears after drawing.

If the \Cffa000Use Selected Alpha\Cc0c0c0 switch is pressed, the \Cffa000Create Radial Gradient\Cc0c0c0 button draws the selected alpha to size, instead of its normal gradient shape.

[fontSetSizeSmall]Gradient Maker Revision A, created by Dave Bushnell

// ======================== SUBROUTINES FOLLOW
[routineDef,DrawLinearGradient,
	[if,[var,ShowMessage],
		[routineCall,DupeLayer]
	] // end if
	[varSet,ldocWidth,document:modifiers:width]
	[varSet,ldocHeight,document:modifiers:height]
	[varSet,lVscale,max(ldocWidth,ldocHeight)]
	[varSet,NumUndos,0]
	[iPress,tool:depthBrush]
	[iPress,stroke:dragRect]
	[iUnPress,alpha:max]
	[iPress,draw:zadd][iUnPress,draw:zadd]
	[iPress,draw:rgb]
	[iSet,draw:rgbintensity,0]
	[iSet,alpha:itemInfo,27]
	
	[if,(OrthoSnap),
		[if,(abs(lH2-lH1) > abs(lV2-lV1)),
			[varSet,lV2,lV1]
		, // else
			[varSet,lH2,lH1]
		] // end if
	] // end if
	
	[varSet,lmidH,[interpolate,.5,lH1,lH2]]
	[varSet,lmidV,[interpolate,.5,lV1,lV2]]
	[canvasClick,lMidH,lMidV,lH2,lV2]
	[iSet,draw:rgbintensity,99]

	[transformGet,transData(0),transData(1),transData(2), transData(3),transData(4),transData(5), transData(6),transData(7),transData(8)]
	[transformSet,transData(0),transData(1),-4095, transData(3)*sqrt2,lVscale,transData(5)*sqrt2, 0,0,transData(8)-90] 
	[varInc,NumUndos]

	[varSet,lix,1]
	[loop,(lVscale/transData(3)/2),
		[iPress,transform:snapshotobject]
		[iSet,alpha:itemInfo,0]
		[transformSet, transData(0)-(lix*2*sqrt2*transData(3)*sin(transData(8))), transData(1)-(lix*2*sqrt2*transData(3)*cos(transData(8))),-4095, transData(3)*sqrt2+.7,lVscale,transData(5)*sqrt2, 0,0,transData(8)-90]
		[varInc,NumUndos] 
		[varInc,lix]
	]
,lH1,lV1,lH2,lV2] // end routine DrawLinearGradient

[routineDef,DrawRadialGradient,
	[iPress,tool:depthBrush]
	[iPress,stroke:dragRect]
	[iUnPress,alpha:max]
	[iPress,draw:zadd][iUnPress,draw:zadd]
	[iPress,draw:rgb]
	[iSet,draw:rgbintensity,0]
	[if,UseCurrentAlpha,,[iSet,alpha:itemInfo,37]]
	
	[if,(OrthoSnap),
		[if,(abs(lH2-lH1) > abs(lV2-lV1)),
			[varSet,lV2,lV1]
		, // else
			[varSet,lH2,lH1]
		] // end if
	] // end if

	[canvasClick,lH1,lV1,lH2,lV2]
	[iSet,draw:rgbintensity,99]
	[transformGet,transData(0),transData(1),transData(2), transData(3),transData(4),transData(5), transData(6),transData(7),transData(8)]
	[transformSet,transData(0),transData(1),-4095, transData(3)*sqrt2,transData(4)*sqrt2,transData(5)*sqrt2, 0,0,transData(8)] 
	
,lH1,lV1,lH2,lV2] // end routine DrawRadialGradient

[RoutineDef,ZeroAdjust,
	[if,(abs(lH2-lH1)<4) && (abs(lV2-lV1)<4),[varSet,lV2,lV1+4]]
,lH1,lV1,lH2,lV2] // end ZeroAdjust routine

[routineDef,StoreSettings,
	[varSet,SettingsData(0),tool:itemInfo]
	[varSet,SettingsData(10),texture:itemInfo]
	[iPress,tool:depthBrush]
	[varSet,SettingsData(1),stroke:itemInfo]
	[varSet,SettingsData(2),[iGet,alpha:max]]
	[varSet,SettingsData(3),0]
	[varSet,lix,4356]
	[loop,3,
		[if,[iGet,lix],[varSet,SettingsData(3),lix]]
		[varInc,lix]
	]
	[varSet,SettingsData(4),0]
	[varSet,lix,4359]
	[loop,3,
		[if,([iGet,lix]),[varSet,SettingsData(4),lix]]
		[varInc,lix]
	]
	[varSet,SettingsData(5),draw:rgbintensity]
	[varSet,SettingsData(6),alpha:itemInfo]
	[varSet,SettingsData(7),color:r]
	[varSet,SettingsData(8),color:g]
	[varSet,SettingsData(9),color:b]
	[varSet,SettingsData(11),material:itemInfo]
	[iSet,texture:itemInfo,SettingsData(10)]
] // end routine StoreSettings
[routineDef,RecallSettings,
	[iPress,tool:depthBrush]
	[iSet,stroke:itemInfo,SettingsData(1)]
	[if,(SettingsData(2)),[iPress,alpha:max],[iUnPress,alpha:max]]
	[if,(SettingsData(3)>0),[iPress,SettingsData(3)]]
	[if,(SettingsData(4)>0),[iPress,SettingsData(4)]]
	[iSet,draw:rgbintensity,SettingsData(5)]
	[iSet,tool:itemInfo,SettingsData(0)]
	[iSet,alpha:itemInfo,SettingsData(6)]
	[iColorSet,SettingsData(7),SettingsData(8),SettingsData(9)]
	[iSet,texture:itemInfo,SettingsData(10)]
	[iSet,material:itemInfo,SettingsData(11)]
	[varSet,lix,0]
] // end routine RecallSettings
[RoutineDef,DupeLayer,
	[iSet,layer:modifiers:displacez,1]
	[iPress,layer:inventory:duplicatelayer]
	[iSet,layer:modifiers:displacez,-1]
] // end DupeLayer routine
[RoutineDef,UndoLinearGradient,
		[if,[var,ShowMessage],
		[if,[MessageYesNo,"Are you satisfied with this gradient? (Press YES to keep, NO to remove)","Gradient Cannot be Undone: Keep or Remove?"],
			[iKeyPress,13,[iPress,layer:inventory:mergelayerdown]]		
		, // else (NO)
			[iPress,layer:inventory:movelayerup]
			[iKeyPress,13,[iPress,layer:inventory:deletelayer]]
			[iSet,layer:modifiers:displacez,-1]
		] // end if
	] // end if
] // end UndoLinearGradient routine
[RoutineDef,UndoRadialGradient,
		[if,[var,ShowMessage],
		[if,[MessageYesNo,"Are you satisfied with this gradient? (Press YES to keep, NO to remove)","Keep or Remove?"],, // else (NO)
			[iPress,document:undo]
		] // end if
	] // end if
] // end UndoRadialGradient routine